﻿/*----------------------------------------------------
 * Author : Tharinduni Udugamasuriya
 * FinCon dll for Handeling DB 
 * DBList : To geth the Database Engine List
 ---------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Sql;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DSPL.Contracts;
using DSPL.Contracts.Common;
using DSPL.Contracts.Interfaces;
using Microsoft.Practices.Unity;

namespace DSPL.Common
{
    /// <summary>
    /// Interaction logic for DBList.xaml
    /// </summary>
    public partial class DBList : Window
    {

        public string DBEngine;
        IMessageService messageService;

        public DBList()
        {
            IUnityContainer container = GlobalVariables.GlobalUnityContainer;
            if (container != null)
            {
                messageService = container.Resolve<IMessageService>();
            } 
            InitializeComponent();
        }

        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            try
            {
                Mouse.OverrideCursor = Cursors.Wait;
                getServerList();
                Mouse.OverrideCursor = null;
            }
            catch (Exception)
            {
                messageService.ShowMessage(MessageText.ServerListLoadError, MessageType.ErrOK);
            }
        }

        private void getServerList()
        {
            try
            {
                SqlDataSourceEnumerator instance;
                instance = SqlDataSourceEnumerator.Instance;
                DataTable dtServerList = instance.GetDataSources();
                if (dtServerList != null)
                {
                    if (dtServerList.Rows.Count > 0)
                    {
                        string[] ServerList = null;
                        ServerList = new string[] { };

                        TreeViewItem tvi = new TreeViewItem();
                        tvi.Header = "Database Engine(s)";

                        foreach (DataRow dr in dtServerList.Rows)
                        {
                            string dispText = "";
                            dispText = dr["ServerName"].ToString().Trim();
                            if (dr["InstanceName"].ToString().Trim().Length > 0)
                            {
                                dispText += "\\" + dr["InstanceName"].ToString().Trim();
                            }
                            tvi.Items.Add(dispText);
                        }

                        tvServerList.Items.Add(tvi);
                        btnOk.IsEnabled = true;
                    }
                    else
                    {
                        TreeViewItem tvi = new TreeViewItem();
                        tvi.Header = "Database Engine(s) Not Found";
                        tvServerList.Items.Add(tvi);
                        btnOk.IsEnabled = false;
                    }
                }
                else
                {
                    TreeViewItem tvi = new TreeViewItem();
                    tvi.Header = "Database Engine(s) Not Found";
                    tvServerList.Items.Add(tvi);
                    btnOk.IsEnabled = false;
                }
            }
            catch (Exception ex)
            {
                messageService.ShowException(ex.Message.ToString(), new StackTrace().GetFrame(1).GetMethod().Name);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            DBEngine = "";
            this.Close();
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (tvServerList.Items.Count > 0)
                {
                    if (tvServerList.SelectedItem == null)
                    {
                        messageService.ShowMessage(MessageText.DBNotSelected, MessageType.WarningOK);
                        DBEngine = "";
                    }
                    else if (tvServerList.SelectedItem.ToString().Trim() == "Database Engine(s)")
                    {
                        messageService.ShowMessage(MessageText.DBNotSelected, MessageType.WarningOK);
                        DBEngine = "";
                    }
                    else
                    {
                        DBEngine = tvServerList.SelectedItem.ToString().Trim();
                    }
                }
                this.Close();
            }
            catch (Exception ex)
            {
                messageService.ShowException(ex.Message.ToString(), new StackTrace().GetFrame(1).GetMethod().Name);
            }
        }
    }
}
